package com.zhiche.lisa.lspm.service.driver.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.lspm.dao.mapper.driver.DriverCarrierBindMapper;
import com.zhiche.lisa.lspm.dao.model.driver.DriverCarrierBind;
import com.zhiche.lisa.lspm.dao.model.driver.DriverCarrierBindLog;
import com.zhiche.lisa.lspm.service.driver.IDriverCarrierBindLogService;
import com.zhiche.lisa.lspm.service.driver.IDriverCarrierBindService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 司机与车头绑定关系 服务实现类
 * </p>
 *
 * @author qichao
 * @since 2018-06-27
 */
@Service
public class DriverCarrierBindServiceImpl extends ServiceImpl<DriverCarrierBindMapper, DriverCarrierBind> implements IDriverCarrierBindService {

    @Autowired
    private IDriverCarrierBindLogService driverCarrierBindLogService;

    @Override
    public boolean bindDriverCarrier(Long driverId, Long carrierId,Long tenantId) {
        unbindDriverCarrier(driverId,carrierId); //先解绑

        DriverCarrierBind driverCarrierBind = new DriverCarrierBind();
        driverCarrierBind.setDriverId(driverId);
        driverCarrierBind.setCarrierId(carrierId);
        driverCarrierBind.setTenantId(tenantId);
//        carrierTrailerBind.setCreator(AccountUtil.getLoginAccountName());
//        carrierTrailerBind.setCreator();

        DriverCarrierBindLog driverCarrierBindlog = new DriverCarrierBindLog();
        BeanUtils.copyProperties(driverCarrierBind,driverCarrierBindlog);
        driverCarrierBindlog.setTenantId(tenantId);
        return this.insert(driverCarrierBind) && driverCarrierBindLogService.insert(driverCarrierBindlog);
    }

    @Override
    public boolean unbindDriverCarrier(Long driverId, Long carrierId) {
        Wrapper<DriverCarrierBind> ew = new EntityWrapper<>();
        ew.eq("driver_id",driverId).or().eq("carrier_id",carrierId);
        return this.delete(ew);
    }

    @Override
    public DriverCarrierBind getCarrierBindByCarrierIdNoDriId(Long carrierId,Long driverId) {
        DriverCarrierBind driverCarrierBind = null;
        EntityWrapper<DriverCarrierBind> ew = new EntityWrapper<>();
        ew.eq("carrier_id",carrierId).ne("driver_id",driverId);
        List<DriverCarrierBind> lists = this.selectList(ew);
        if(CollectionUtils.isNotEmpty(lists)){
            driverCarrierBind = lists.get(0);
        }
        return  driverCarrierBind;
    }

    @Override
    public DriverCarrierBind getCarrierBindByDriverId(Long driverId) {
        DriverCarrierBind driverCarrierBind = null;
        EntityWrapper<DriverCarrierBind> ew = new EntityWrapper<>();
        ew.eq("driver_id",driverId);
        List<DriverCarrierBind> lists = this.selectList(ew);
        if(CollectionUtils.isNotEmpty(lists)){
            driverCarrierBind = lists.get(0);
        }
        return  driverCarrierBind;
    }

    @Override
    public DriverCarrierBind getCarrierBindByCarIdAndDriId(Long carrierId, Long driverId) {
        DriverCarrierBind driverCarrierBind = null;
        EntityWrapper<DriverCarrierBind> ew = new EntityWrapper<>();
        ew.eq("carrier_id",carrierId).eq("driver_id",driverId);
        List<DriverCarrierBind> lists = this.selectList(ew);
        if(CollectionUtils.isNotEmpty(lists)){
            driverCarrierBind = lists.get(0);
        }
        return  driverCarrierBind;
    }

    @Override
    public List<DriverCarrierBind> selectListNoTenant(EntityWrapper<DriverCarrierBind> dcbEW) {
        return baseMapper.selectListNoTenant(dcbEW);
    }
}
